//
//  MYGInputView.swift
//  MYDoctor
//
//  Created by 小火科技 on 2024/4/26.
//

import UIKit
import BRPickerView

class MYGInputView: UIView {

    var inputDidChange:((String) -> ())?
    var selelctView:(() -> ())?
    
    
    init(title:String,subTitle:String,isShow:Bool,frame: CGRect) {
        super.init(frame: frame)
        configUI()
        nameLabel.text = title
        textFiled.placeholder = subTitle
        selectButton.isHidden = !isShow
        contentImageView.isHidden = !isShow
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func configUI(){
       
        addSubviews([nameLabel,textFiled,contentImageView])
        addSubview(selectButton)
 
        
        nameLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(15)
            make.centerY.equalToSuperview()
            make.width.equalTo(80)
            make.height.equalTo(40)
        }
        
        textFiled.snp.makeConstraints { make in
            make.centerY.equalTo(nameLabel)
            make.left.equalTo(nameLabel.snp.right).offset(17)
            make.height.equalTo(40)
            make.right.equalToSuperview().offset(-20)
        }
        
        contentImageView.snp.makeConstraints { make in
            make.width.height.equalTo(12)
            make.centerY.equalToSuperview()
            make.right.equalToSuperview()
        }
        
        selectButton.snp.makeConstraints { make in
            make.top.right.bottom.equalToSuperview()
            make.left.equalTo(textFiled)
        }
        addSubview(line)
        line.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(0.5)
        }
    }
    
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = .medium(16)
        label.textColor = .rgba(28, 28, 28, 1)
        label.text = "就诊人"
//        label.backgroundColor = .yellow
        return label
    }()
    
    
    
     lazy var textFiled: UITextField = {
        let tf = UITextField()
         tf.textAlignment = .right
        tf.textColor = .rgba(51, 51, 51)
        tf.font = .regular(16)
        tf.placeholder = "请输入 "
//        tf.backgroundColor = .red
        tf.addTarget(self, action: #selector(textFiledEditingDidChange), for: .editingChanged)
        return tf
    }()
    
    
    private lazy var contentImageView: UIImageView = {
        let imgV = UIImageView()
        imgV.image = UIImage(named: "re_next")
        imgV.layer.masksToBounds = true
        imgV.layer.cornerRadius = 4
        return imgV
    }()
    
    private lazy var selectButton: UIButton = {
        let btn = UIButton()
        btn.backgroundColor = .clear
        btn.addTarget(self, action: #selector(clickSelectButton), for: .touchUpInside)
        return btn
    }()
    
    private lazy var line: UIView = {
        let l = UIView()
        l.backgroundColor = .rgba(238, 238, 238, 1)
        return l
    }()
    
}

extension MYGInputView {
    
    @objc
    func clickSelectButton(){
        
        print("点击了选择")
       selelctView?()
        
    }
    
    @objc func textFiledEditingDidChange() {
        inputDidChange?(textFiled.text ?? "")
    }
    
}
